package com.itboye.core.utils;

import java.util.HashMap;
import java.util.Map;

import com.itboye.mengwu.app.MainApplication;

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
/**
 * 
 * @author Young
 *
 * @param <T>
 */
public class MyJsonRequest<T> extends XJsonRequest<T> {

	private MyJsonRequest(String url, XRequestListener<T> listener, XErrorListener errlistener) {
		super(url, listener, errlistener);

	}

	private MyJsonRequest(int method, String url, XRequestListener<T> listener, XErrorListener errlistener) {
		super(method, url, listener, errlistener);

	}

	public static class Builder<T> {

		private String url = "URL";
		private XRequestListener<T> listener;
		private XErrorListener errlistener;
		private String typekey;
		private String apiVer;

		private Integer method;
		private Map<String, String> map;
		private Map<String, Object> userParam;
		private String time = TimesUtils.getStamp();
		
		/**
		 * 获取App的版本号
		 * @return 返回App的版本号
		 */
		private int getVersionCode()
		{
		    int appVersion=0;
			try
			{

				PackageManager pm=MainApplication.context.getPackageManager();
				PackageInfo info = pm.getPackageInfo(MainApplication.context.getPackageName(), 0);
				appVersion=info.versionCode;
			}catch(Exception e)
			{
				
			}
			return appVersion;
		}
		
		
		String appv=String.valueOf(getVersionCode());
		
		

		public Builder() {
			map = new HashMap<String, String>();
			userParam=new HashMap<String, Object>();
			map.put(NetPublicConstant.Grant_Type_Key, NetPublicConstant.Grant_Type_Value);
			map.put(NetPublicConstant.Client_Id_Key, NetPublicConstant.Client_Id_Value);
			map.put(NetPublicConstant.Client_Secret_Key, NetPublicConstant.Client_Secret_Value);
//			
//			
			map.put(NetPublicConstant.App_Version, appv);
			map.put(NetPublicConstant.App_Type, "android");
			
			map.put(NetPublicConstant.Client_Secret_Key, NetPublicConstant.Client_Secret_Value);
			// 公共参数
			map.put(NetPublicConstant.Time_Key, time);
			map.put(NetPublicConstant.Alg_Key, NetPublicConstant.Alg_Value);

			map.put(NetPublicConstant.Notify_Id_Key, time);
			map.put(NetPublicConstant.Access_Token_Key, NetPublicConstant.Client_Id_Value);
		}

		public Builder<T> param(String k,String v) {
			userParam.put(k, v);
			return this;
		}
		/**
		 * 
		 * @param k
		 * @param v
		 * @return
		 */
		public Builder<T> param(String k,Object v) {
			userParam.put(k, v);
			return this;
		}
		public Builder<T> url(String url) {
			this.url = url;
			return this;
		}

		public Builder<T> requestListener(XRequestListener<T> listener) {
			this.listener = listener;
			return this;
		}

		public Builder<T> errorListener(XErrorListener errlistener) {
			this.errlistener = errlistener;
			return this;
		}

		public Builder<T> typeKey(String typekey) {
			this.typekey = typekey;
			return this;
		}

		public Builder<T> apiVer(String ver) {
			this.apiVer = ver;
			return this;
		}

		public Builder<T> requestMethod(int method) {
			this.method=method;
			return this;
		}
		/**
		 * 创建 MyJsonRequest对象
		 * @return
		 */
		public MyJsonRequest<T> build() {
			if (listener == null) {
				System.err.println("XRequestListener == null !");
			}
			if (errlistener == null) {
				System.err.println("ErrorListener == null !");
			}

			if (null == typekey) {
				throw new RuntimeException("typekey can not be null ! you should use typeKey(String typekey) ");
			}
			
			if (null == apiVer) {
				throw new RuntimeException("apiVer can not be null ! you should use apiVer(String ver) ");
			}

//			printLogDetail("rawRequest","-->"+typekey+":\n"+userParam.toString());
		//	DebugLog.v("rawRequest", "-->"+typekey+":\n"+userParam.toString());
			String param =  new ParamDeal().dataEncrypt(userParam);
			String sign = signInfo(param);

			MyJsonRequest<T> request = new MyJsonRequest<T>(url, listener, errlistener);
			request.addParams(map)
					.addParam(NetPublicConstant.Type_Key, typekey)
					.addParam(NetPublicConstant.API_Ver_Key, apiVer)
					
					.addParam(NetPublicConstant.Data_Key, param)
					.addParam(NetPublicConstant.Sign_Key, sign);

			if (method!=null) {
				request.setMethod(method);
			}
			return request;

		}

		private String signInfo(String userInf) {
			ParamDeal operate = new ParamDeal();
			String sign = null;//operate.getMD5Sign(time, typekey, userInf, NetPublicConstant.Client_Id_Value, time);

			return sign;
		}

//		private String decodeUserInfo() {
//			 
//			String data = new ParamDeal().dataEncrypt(userParam);
//			return data;
//		}

	}

}
